﻿Public Class PasDasStructureManager

    Public Property dimplomaTypePasVASIKO As DiplomaType
    Public Property dimplomaTypePasANOTERO As DiplomaType
    Public Property dimplomaTypePasANOTATO As DiplomaType

    Private Property lessons As List(Of Lesson)

    Private _lsGr10 As Lesson
    Private _lsGr20 As Lesson
    Private _lsGr30 As Lesson
    Private _lsC030 As Lesson

    Public Function isUpdateNeeded() As Boolean

        Return Me.dimplomaTypePasVASIKO Is Nothing

    End Function

    Sub New()

        lessons = LessonDataUtils.findList()

        _lsGr10 = lessons.First(Function(x)
                                    Return x.LessonCode = "GR10"
                                End Function)

        _lsGr20 = lessons.First(Function(x)
                                    Return x.LessonCode = "GR20"
                                End Function)

        _lsGr30 = lessons.First(Function(x)
                                    Return x.LessonCode = "GR30"
                                End Function)

        _lsC030 = lessons.First(Function(x)
                                    Return x.LessonCode = "C030"
                                End Function)

        Me.dimplomaTypePasVASIKO = DiplomaTypeDataUtils.findOne("DiplomaType= 'ΠΑΣ - ΒΑΣΙΚΟ'")
        Me.dimplomaTypePasANOTERO = DiplomaTypeDataUtils.findByKey(4)
        Me.dimplomaTypePasANOTATO = DiplomaTypeDataUtils.findByKey(5)

    End Sub


    Public Sub updatePasDasStructure()

        updatePasStructure()
        updateDASStructure()

    End Sub

    ''' <summary>
    ''' Updates the PAS and DAS structures
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub updatePasStructure()

        If Not isUpdateNeeded() Then Return

        Dim certificationType As CertificationType
        Dim cr As CertificationRequirement

        Dim pas1lessons As String

        Dim ls835 As Lesson = lessons.First(Function(x)
                                                Return x.LessonCode = "835"
                                            End Function
                                            )


        Dim ls570 As Lesson = lessons.First(Function(x)
                                                Return x.LessonCode = "570"
                                            End Function
                                            )


        Dim ls925 As Lesson = lessons.First(Function(x)
                                                Return x.LessonCode = "925"
                                            End Function
                                           )


        Call deleteDiplomaType("ΠΑΣ ΕΛΛΑΔΑΣ - ΑΝΩΤΕΡΟ")
        Call deleteDiplomaType("ΠΑΣ ΕΛΛΑΔΑΣ - ΑΝΩΤΑΤΟ")
        Call deleteDiplomaType("ΔΙΠΛΩΜΑ ΑΣΦΑΛΙΣΤΙΚΩΝ ΣΠΟΥΔΩΝ - ΕΛΛΑΔΑ")

        Me.dimplomaTypePasVASIKO = DiplomaTypeDataUtils.findOne("DiplomaType= 'ΠΑΣ - ΒΑΣΙΚΟ'")

        If Me.dimplomaTypePasVASIKO Is Nothing Then

            Me.dimplomaTypePasVASIKO = DiplomaTypeFactory.Create
            Me.dimplomaTypePasVASIKO.DiplomaType = "ΠΑΣ - ΒΑΣΙΚΟ"
            Me.dimplomaTypePasVASIKO.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PAS

            pas1lessons = "C010,C020,C030,C25,C45"

            certificationType = CertificationTypeFactory.Create
            certificationType.CertificationType = "ΠΑΣ - ΒΑΣΙΚΟ"
            certificationType.DiplomaSuperTypeId = 2
            certificationType.NumberOptionalRequired = 1
            For Each ls As Lesson In lessons
                'Debug.Print(ls.LessonCode)
                'If ls.LessonCode = "945" Then Stop
                If pas1lessons.Contains(ls.LessonCode) Then
                    cr = CertificationRequirementFactory.Create
                    cr.LessonId = ls.LessonId
                    cr.IsOptional = (ls.LessonCode <> "C010") AndAlso (ls.LessonCode <> "C020") AndAlso (ls.LessonCode <> "C030")
                    certificationType.AddCertificationRequirement(cr)
                End If
            Next

            certificationType.getLessonRequirementByCode("C010").LessonIdAlternative = _lsGr10.LessonId
            certificationType.getLessonRequirementByCode("C020").LessonIdAlternative = _lsGr20.LessonId
            certificationType.getLessonRequirementByCode("C030").LessonIdAlternative = _lsGr30.LessonId

            Me.dimplomaTypePasVASIKO.AddCertificationRequirement(certificationType)
            DiplomaTypeDataUtils.saveDiplomaType(dimplomaTypePasVASIKO)


        End If

      

        dimplomaTypePasANOTERO.DiplomaType = "ΠΑΣ - ΑΝΩΤΕΡΟ"
        dimplomaTypePasANOTERO.DiplomaSuperTypeId = 2

        Dim anoteroCertReq As CertificationType = dimplomaTypePasANOTERO.DiplomaRequirements(0).Certification
        anoteroCertReq.NumberOptionalRequired = 2
        anoteroCertReq.AddLessonRequirement(ls835, True)
        anoteroCertReq.AddLessonRequirement(ls570, True)
        anoteroCertReq.AddLessonRequirement(ls925, True)

        anoteroCertReq.getLessonRequirementByCode("C010").LessonIdAlternative = _lsGr10.LessonId
        anoteroCertReq.getLessonRequirementByCode("C020").LessonIdAlternative = _lsGr20.LessonId
        anoteroCertReq.getLessonRequirementByCode("C030").LessonIdAlternative = _lsGr30.LessonId

        DiplomaTypeDataUtils.saveDiplomaType(dimplomaTypePasANOTERO)

        dimplomaTypePasANOTATO.DiplomaType = "ΠΑΣ - ΑΝΩΤΑΤΟ"
        dimplomaTypePasANOTATO.DiplomaSuperTypeId = 2
        Dim anotatoCertReq As CertificationType = dimplomaTypePasANOTATO.DiplomaRequirements(0).Certification
        anotatoCertReq.NumberOptionalRequired = 4
        anotatoCertReq.AddLessonRequirement(ls835, True)
        anotatoCertReq.AddLessonRequirement(ls570, True)
        anotatoCertReq.AddLessonRequirement(ls925, True)
        anotatoCertReq.getLessonRequirementByCode("C010").LessonIdAlternative = _lsGr10.LessonId
        anotatoCertReq.getLessonRequirementByCode("C020").LessonIdAlternative = _lsGr20.LessonId
        anotatoCertReq.getLessonRequirementByCode("C030").LessonIdAlternative = _lsGr30.LessonId
        anotatoCertReq.CertificationRequirements.ToList.ForEach(Sub(x)
                                                                    x.IsOptional = x.LessonCode <> "C010" AndAlso x.LessonCode <> "C020" AndAlso x.LessonCode <> "C030"
                                                                End Sub)

        DiplomaTypeDataUtils.saveDiplomaType(dimplomaTypePasANOTATO)

        ModelContext.CurrentDBUtils.executeSQL("delete FROM [iicdb].[dbo].[CertificationType] where CertificationTypeId in (6,7)")

    End Sub

    Public Sub updateDASStructure()

        Dim dimplomaTypeDAS As DiplomaType = DiplomaTypeDataUtils.findOne("DiplomaType= 'ΔΙΠΛΩΜΑ ΑΣΦΑΛΙΣΤΙΚΩΝ ΣΠΟΥΔΩΝ'")

        For Each d As DiplomaRequirement In dimplomaTypeDAS.DiplomaRequirements
            For Each cr As CertificationRequirement In d.Certification.CertificationRequirements
                If cr.LessonCode = "C010" Then
                    cr.LessonIdAlternative = Me._lsGr10.LessonId
                End If
                If cr.LessonCode = "C020" Then
                    cr.LessonIdAlternative = Me._lsGr20.LessonId
                End If
                If cr.LessonCode = "C030" Then
                    cr.LessonIdAlternative = Me._lsGr30.LessonId
                End If
                If cr.LessonCode = "530" Then
                    cr.LessonIdAlternative = Me._lsGr30.LessonId
                    cr.LessonIdAlternative1 = Me._lsC030.LessonId
                End If

                If cr.LessonCode = "765" Then
                    cr.LessonIdAlternative = lessons.First(Function(x)
                                                               Return x.LessonCode = "610"
                                                           End Function
                                            ).LessonId

                End If

                If cr.LessonCode = "745" Then
                    cr.LessonIdAlternative = lessons.First(Function(x)
                                                               Return x.LessonCode = "625"
                                                           End Function
                                            ).LessonId

                End If
                If cr.LessonCode = "735" Then
                    cr.LessonIdAlternative = lessons.First(Function(x)
                                                               Return x.LessonCode = "560"
                                                           End Function
                                            ).LessonId

                End If

            Next
        Next

        DiplomaTypeDataUtils.saveDiplomaType(dimplomaTypeDAS)

       

    End Sub

    Private Shared Sub deleteDiplomaType(ByVal diplomaTypeName As String)
        Dim dimplomaType As DiplomaType
        dimplomaType = DiplomaTypeDataUtils.findOne(" DiplomaType={0}", diplomaTypeName)
        If dimplomaType Is Nothing Then
            Return
        Else
            DiplomaTypeDataUtils.deleteDiplomaType(dimplomaType)
            dimplomaType = Nothing
        End If
    End Sub

End Class
